<?php
declare (strict_types=1);

namespace app\admin\model\base;

use app\BaseModel;
use app\BaseService;
use think\facade\Cache;

/**
 * 用户表模型
 */
class User extends BaseModel
{

    /**
     * 获取用户列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @param string $field
     * @return array
     */
    public static function getList(array $where, int $page, int $limit, string $orderby = '', string $field = '*'): array
    {
        //查询
        $data = self::field($field)
            ->where($where)
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        //遍历数组
        foreach ($data['data'] as &$v) {
            unset($v['safe_code']);
            unset($v['password']);
        }
        return $data;
    }

    /**
     * 根据Id查询用户信息(微信信息只关联主体微信公众号)
     * @param int $user_id
     * @return array
     */
    public static function getIdUserInfo(int $user_id): array
    {
        //查询数据库
        $field = 'id,uuid,area_code,phone,unionid,openid,subscribe,nickname,avatar,sex,latitude,longitude';
        $data = self::field($field)
            ->where(['id' => $user_id])
            ->cache(true, 60)
            ->find();
        return $data ? $data->toArray() : [];
    }

    /**
     * 根据unionid查询用户信息(微信信息只关联主体微信公众号)
     * @param string $unionid
     * @return array
     */
    public static function getUnionidUserInfo(string $unionid): array
    {
        //查询数据库
        $field = 'id,uuid,area_code,phone,unionid,openid,subscribe,nickname,avatar,sex,latitude,longitude';
        $data = self::field($field)
            ->where(['unionid' => $unionid])
            ->cache(true, 600)
            ->find();
        return $data ? $data->toArray() : [];
    }

    /**
     * 写入用户微信关注信息
     * @param string $openid
     * @param int $subscribe
     * @return User
     */
    public static function writeWechatSubscribe(string $openid, int $subscribe): User
    {
        return self::update(['subscribe' => $subscribe], ['openid' => $openid]);
    }

    /**
     * 写入用户地理位置信息
     * @param int $user_id
     * @param string $mac_id
     * @param string $Latitude
     * @param string $Longitude
     * @return bool
     */
    public static function userLocationRedis(int $user_id, string $mac_id, string $Latitude, string $Longitude): bool
    {
        $data = [
            'mac_id' => $mac_id,
            'latitude' => $Latitude,
            'longitude' => $Longitude,
        ];
        //存入缓存供出袋时验证
        $tKey = BaseService::generateLocationKey($user_id);
        //用户最新的token写入缓存
        Cache::set($tKey, $data, 6000);
        //保存地理位置数据库
        if ($Latitude or $Longitude) {
            self::update($data, ['id' => $user_id]);
        }
        return true;
    }

    /**
     * 写入用户地理位置信息
     * @param string $openid
     * @param string $Latitude
     * @param string $Longitude
     * @return User
     */
    public static function writeOpenidLocation(string $openid, string $Latitude, string $Longitude): User
    {
        return self::update(['latitude' => $Latitude, 'longitude' => $Longitude], ['openid' => $openid]);
    }

}




